package com.itheima.studenthomework.service;

import com.itheima.studenthomework.dao.StudentDao;
import com.itheima.studenthomework.domain.Student;

import java.util.ArrayList;

public class StudentService {
    public boolean addStudent(Student student) {
        StudentDao std = new StudentDao();

        boolean flag = std.addStudent(student);
        return flag;

    }

    public int isExist(String id){
        int index = -1;
        StudentDao std = new StudentDao();
        ArrayList<Student> list = std.giveList();
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if(id.equals(stu.getId())){
                index = i;
                break;
            }
        }

        return index;
    }

    public ArrayList<Student> findStudent() {
        StudentDao std = new StudentDao();
        ArrayList<Student> list = std.giveList();
        if(list.size() != 0){
            return list;
        }
        return null;
    }

    public boolean updateStudent(Student stu) {
        StudentDao std = new StudentDao();
        boolean flag = std.updateStudent(stu);
        return flag;

    }

    public boolean deleteStudent(int index) {
        StudentDao std = new StudentDao();
        boolean flag = std.deleteStudent(index);
        return flag;
    }

    public Student findOneStudent(int index) {
        StudentDao std = new StudentDao();
        Student student = std.findOneStudent(index);
        return student;
    }
}
